#include<vector>
#include<iostream>
using std::vector;


class Solution {
public:
    vector<vector<int>> permute(vector<int>& nums) {
        vector<vector<int>> permutations;
        permuteNext(permutations, nums, 0);
        return permutations;
    }

    void permuteNext(vector<vector<int>>& results, vector<int>& nums, int left) {
        if (left == nums.size()) {
            vector<int> a(nums);
            results.push_back(a);
            return;
        }

        for (int i = left; i < nums.size(); i++) {
            swap(nums, left, i);
            permuteNext(results, nums, left+1);
            swap(nums, left, i);
        }
        
    }

    void swap(vector<int>& nums, int i, int j) {
        auto temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
};